/*
 * Copyright (c) 2023 Cshoo Org. All Rights Reserved.
 */

package org.cshoo.tattoo.datacenter.domain.repository;

import org.cshoo.tattoo.datacenter.domain.po.Message;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.Modifying;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.CrudRepository;

import javax.transaction.Transactional;

/**
 * @author 杨翼
 * @since 1.0
 */
public interface IMessageRepository extends CrudRepository<Message, Long>  {
    /**
     * 根据编号查询message
     *
     * @param code 编号
     * @return lookup
     */
    Message findByCode(String code);

    /**
     * 分页查询message
     *
     * @param keyword 查询关键字
     * @param pageable             分页对象
     * @return lookup分页列表
     */
    @Query("SELECT message FROM Message message WHERE message.code like %:#{#keyword}% OR message.name LIKE %:#{#keyword}%")
    Page<Message> queryMessage(String keyword, Pageable pageable);

    /**
     * 修改message基本信息
     * 只修改name，description，defaultContent等信息
     *
     * @param message message po
     */
    @Query("update Message message set message.name = :#{#message.name}, message.description = :#{#message.description}, message.defaultContent = :#{#message.defaultContent}, message.lastModifiedBy = :#{#message.lastModifiedBy}, message.lastModifiedDate = :#{#message.lastModifiedDate} where message.id = :#{#message.id}")
    @Transactional
    @Modifying
    void updateMessage(Message message);
}
